package com.xyf.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xyf.sys.config.MiniprogramConfig;
import com.xyf.sys.domain.Goods;
import com.xyf.sys.domain.Order;
import com.xyf.sys.domain.RegimentAssist;
import com.xyf.sys.service.IGoodsService;
import com.xyf.sys.util.R;
import com.xyf.sys.vo.GoodsListParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author R-H
 * @Date 2024/8/311111
 * @Description
 */
@RestController
@RequestMapping("/goods")
@Slf4j
@Api(tags = "商品")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    @Qualifier(value = "clientConfig")
    private MiniprogramConfig.MiniConfig clientConfig;
    @Autowired
    @Qualifier(value = "manageConfig")
    private MiniprogramConfig.MiniConfig manageConfig;

    @ApiOperation(value = "商品发布", notes = "商品发布")
    @PostMapping("/publish")
    public Object publish(@RequestBody Goods goods, HttpServletRequest request) {
        String appid = request.getHeader("APPID");
        goodsService.add(goods);
        return R.ok();
    }

    @ApiOperation(value = "列表", notes = "列表")
    @PostMapping("/list")
    public Object list(@RequestBody GoodsListParam body, HttpServletRequest request) {
        body.setClient(clientConfig.getAppid().equalsIgnoreCase(request.getHeader("APPID")));
        if (!body.isClient() && body.getQueryDeptId() == null){
            body.setQueryDeptId(Long.parseLong(request.getHeader("DEPTID")));
        }
        PageHelper.startPage(body.getPageNum(), body.getPageSize());
        List<Goods> orderList = goodsService.getList(body);
        return R.ok(new PageInfo(orderList));
    }

    @ApiOperation(value = "商品删除", notes = "商品删除")
    @PostMapping("/remove")
    public Object remove(@RequestBody Goods goods, HttpServletRequest request) {
        String appid = request.getHeader("APPID");
        return R.ok(goodsService.remove(goods.getId(), goods.getUserId()));
    }

    @ApiOperation(value = "商品下架", notes = "商品下架")
    @PostMapping("/down")
    public Object down(@RequestBody Goods goods, HttpServletRequest request) {
        String appid = request.getHeader("APPID");
        return R.ok(goodsService.downGoods(goods.getId(), goods.getUserId()));
    }

    @ApiOperation(value = "商品上架", notes = "商品上架")
    @PostMapping("/up")
    public Object up(@RequestBody Goods goods, HttpServletRequest request) {
        String appid = request.getHeader("APPID");
        return R.ok(goodsService.upGoods(goods.getId(), goods.getUserId()));
    }

    @ApiOperation(value = "跟团列表", notes = "跟团列表")
    @GetMapping("/groupBuyingUserList")
    public Object communityGroupBuying(@RequestParam(required = false) Long queryDeptId, HttpServletRequest request) {
        String appid = request.getHeader("APPID");
        return R.ok(goodsService.queryCommunityGroupBuyingList(queryDeptId));
    }

    @ApiOperation(value = "库存设置", notes = "库存设置")
    @PostMapping("/stockSetting")
    public Object stockSetting(@RequestBody Goods goods, HttpServletRequest request) {
        String appid = request.getHeader("APPID");
        return R.ok(goodsService.updateStock(goods));
    }


    @ApiOperation(value = "查询详情", notes = "查询详情")
    @GetMapping("/info")
    public Object info(@RequestParam Long id, HttpServletRequest request) {
        return R.ok(goodsService.getInfo(id));
    }


    @PostMapping("/fetch")
    public Object fetch(@RequestBody Long[] ids, HttpServletRequest request) {
        return R.ok(goodsService.getList(ids));
    }


}
